﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.BaseLibrary;
using Core.Toolkit.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Spire.Presentation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Core.SimpleTemp.Application.Common
{
    [AutoDi()]
    public class PPT2Html
    {
        private readonly ILogger _logger;
        private readonly TempDirDiHelp _tempDirDiHelp;
        private readonly WebAppOptions _webAppOptions;
        public PPT2Html(ILogger<PPT2Html> logger, TempDirDiHelp tempDirDiHelp, IOptionsMonitor<WebAppOptions> webAppOptions)
        {
            _logger = logger;
            _tempDirDiHelp = tempDirDiHelp;
            _webAppOptions = webAppOptions.CurrentValue;
        }

        private bool DoConvertHtml(string pptPath, string htmlPath)
        {
            _logger.LogInformation("PPT转换HTML开始PPT Path:{pptPath} To HtmlPath:{htmlPath}", pptPath, htmlPath);
            using (Presentation ppt = new Presentation())
            {
                ppt.LoadFromFile(pptPath);
                ppt.SaveToFile(htmlPath, FileFormat.Html);
            }
            _logger.LogInformation("PPT转换HTML结束PPT Path:{pptPath} To HtmlPath:{htmlPath}", pptPath, htmlPath);
            return true;
        }

        /// <summary>
        /// 移除文字
        /// </summary>
        /// <param name="originalHtemlPath"></param>
        /// <param name="newHtmlPath"></param>
        /// <returns></returns>
        private bool HtmlRemoveText(string originalHtemlPath, string newHtmlPath)
        {
            const string replaceText = "Evaluation Warning : The document was created with  Spire.Presentation for .NET";

            _logger.LogInformation("HtmlRemoveText originalHtemlPath:{originalHtemlPath} To newHtmlPath:{newHtmlPath}", originalHtemlPath, newHtmlPath);
            using (StreamWriter sw = new StreamWriter(newHtmlPath, true, System.Text.Encoding.UTF8))
            {
                using (StreamReader sr = new StreamReader(originalHtemlPath, Encoding.UTF8))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        line = line.Replace(replaceText, "");
                        sw.WriteLine(line);
                    }
                }
            }
            System.IO.File.Delete(originalHtemlPath);
            _logger.LogInformation("HtmlRemoveText originalHtemlPath:{originalHtemlPath} To newHtmlPath:{newHtmlPath}", originalHtemlPath, newHtmlPath);
            return true;
        }

        public bool ToHtml(string pptPath, string saveHtmlFilePath)
        {
            string htmlTemp = _tempDirDiHelp.GetFilePath(".html");
            //string htmlPath = Filehelp.GetFilePath(_webAppOptions.FileSaveBasePath) + ".html";
            if (!this.DoConvertHtml(pptPath, htmlTemp))
                return false;
            if (!this.HtmlRemoveText(htmlTemp, saveHtmlFilePath))
                return false;
            return true;
        }
    }
}
